#!/usr/local/bin/perl -w
use strict;
use utf8;
use lib '../lib';

use Data::Dumper;
$Data::Dumper::Deparse = 1;

use NG::Gen::App 'NG::Sys::Loop::Run';

my $app = new NG::Sys::App(logger => "screen");
$app->die_to_logger;
$app->warn_to_logger;

$app->run();

sub on_process {
	my $user = $app->users->create_user;
	print $user->level_name,"\n";
}

sub _b_on_process {
	my $user = $app->users->create_user;
	my $proto = $app->reference("leaders")->get_by_key(1);
	if(my $err = $user->add_leader($proto)){
		$app->logger->error($err);
		return;
	}
	my $base = shift @{$user->leaders->get_by_key(1,1)};
	unless($base){
		$app->logger->error($user->leaders->dump);
		return;
	}
	$proto = $app->reference("units")->get_by_key(1);
	if(my $err = $user->add_unit($base,$proto)){
		$app->logger->error($err);
		return;
	}
	$proto = $app->reference("units")->get_by_key(2);
	if(my $err = $user->add_unit($base,$proto)){
		$app->logger->error($err);
		return;
	}
	if(my $err = $user->add_unit($base,$proto)){
		$app->logger->error($err);
		return;
	}
	if(my $err = $user->add_unit($base,$proto)){
		$app->logger->error($err);
		return;
	}
	if(my $err = $user->add_unit($base,$proto)){
		$app->logger->error($err);
		return;
	}
	$proto = $app->reference("leaders")->get_by_key(2);
	if(my $err = $user->add_leader($proto)){
		$app->logger->error($err);
		return;
	}
	$proto = $app->reference("leaders")->get_by_key(3);
	if(my $err = $user->add_leader($proto)){
		$app->logger->error($err);
		return;
	}
	my $leader2 = shift @{$user->leaders->get_by_key(2,1)};
	my $leader3 = shift @{$user->leaders->get_by_key(3,1)};
	unless($leader2){
		$app->logger->error("no leader2");
		return;
	}
	unless($leader3){
		$app->logger->error("no leader3");
		return;
	}
	$proto = $app->reference("units")->get_by_key(2);
	if(my $err = $user->transfer_unit($base,$leader2,$proto,1)){
		$app->logger->error($err);
		return;
	}
	if(my $err = $user->transfer_unit($base,$leader3,$proto,2)){
		$app->logger->error($err);
		return;
	}
	$proto = $app->reference("items")->get_by_key(1);
	if(my $err = $user->add_item($base,$proto)){
		$app->logger->error($err);
		return;
	}
	if(my $err = $user->add_item($base,$proto)){
		$app->logger->error($err);
		return;
	}
	if(my $err = $user->add_item($base,$proto)){
		$app->logger->error($err);
		return;
	}
	if(my $err = $user->add_item($base,$proto)){
		$app->logger->error($err);
		return;
	}
	if(my $err = $user->add_item($base,$proto)){
		$app->logger->error($err);
		return;
	}

	my $e;
	if(my $err = $user->transfer_item($base,$leader2,$e = $base->items->get_by_pos(0)->entry)){
		$app->logger->error($err);
		return;
	}else{
		$app->logger->info("ok $e");
	}
	if(my $err = $user->transfer_item($base,$leader3,$e = $base->items->get_by_pos(0)->entry)){
		$app->logger->error($err);
		return;
	}else{
		$app->logger->info("ok $e");
	}
	if(my $err = $user->transfer_item($base,$leader2,$e = $base->items->get_by_pos(0)->entry)){
		$app->logger->error($err);
		return;
	}else{
		$app->logger->info("ok $e");
	}
	if(my $err = $user->transfer_item($base,$leader3,$e = $base->items->get_by_pos(0)->entry)){
		$app->logger->error($err);
		return;
	}else{
		$app->logger->info("ok $e");
	}
}

